package com.wlyuan.open;

import com.wlyuan.open.config.*;
import com.wlyuan.sms.client.AliyunSmsClient;
import com.wlyuan.sms.code.SmsCodeClient;
import com.wlyuan.sms.code.SmsCodeProperties;
import com.wlyuan.sms.store.RedissonSmsCodeStore;
import com.wlyuan.web.EnableHibernateValidation;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import springfox.documentation.oas.annotations.EnableOpenApi;

/**
 * @author yuanzheng
 */
@SpringBootApplication
@EnableOpenApi
@EnableDiscoveryClient
@EnableConfigurationProperties({
        OpenPlatformProperties.class,
        RegisterSmsProperties.class,
        UnbindSmsProperties.class,
        BindSmsProperties.class,
        ChangeAppSecretSmsProperties.class
})
@EnableHibernateValidation
public class OpenPlatformWebApplication {
    public static void main(String[] args) {
        System.setProperty("dubbo.application.logger", "log4j2");
        SpringApplication.run(OpenPlatformWebApplication.class, args);
    }

    @Bean(name = "unbindMobileSmsClient")
    public SmsCodeClient unbindSmsClient(RedissonClient redissonClient, UnbindSmsProperties passwordSmsProperties, AliyunSmsClient aliyunSmsClient) {
        SmsCodeProperties codeProperties = new SmsCodeProperties();
        BeanUtils.copyProperties(passwordSmsProperties, codeProperties);
        RedissonSmsCodeStore passwordSmsCodeStore = new RedissonSmsCodeStore(redissonClient, codeProperties);
        return new SmsCodeClient(passwordSmsCodeStore, codeProperties, aliyunSmsClient);
    }

    @Bean(name = "registerSmsClient")
    public SmsCodeClient registerSmsClient(RedissonClient redissonClient, RegisterSmsProperties registerSmsProperties, AliyunSmsClient aliyunSmsClient) {
        SmsCodeProperties codeProperties = new SmsCodeProperties();
        BeanUtils.copyProperties(registerSmsProperties, codeProperties);
        RedissonSmsCodeStore registerSmsCodeStore = new RedissonSmsCodeStore(redissonClient, codeProperties);
        return new SmsCodeClient(registerSmsCodeStore, codeProperties, aliyunSmsClient);
    }

    @Bean(name = "bindSmsClient")
    public SmsCodeClient bindSmsClient(RedissonClient redissonClient, BindSmsProperties bindSmsProperties, AliyunSmsClient aliyunSmsClient) {
        SmsCodeProperties codeProperties = new SmsCodeProperties();
        BeanUtils.copyProperties(bindSmsProperties, codeProperties);
        RedissonSmsCodeStore registerSmsCodeStore = new RedissonSmsCodeStore(redissonClient, codeProperties);
        return new SmsCodeClient(registerSmsCodeStore, codeProperties, aliyunSmsClient);
    }

    @Bean(name = "changeAppSecretSmsClient")
    public SmsCodeClient changeAppSecretSmsClient(RedissonClient redissonClient, ChangeAppSecretSmsProperties changeAppSecretSmsProperties, AliyunSmsClient aliyunSmsClient) {
        SmsCodeProperties codeProperties = new SmsCodeProperties();
        BeanUtils.copyProperties(changeAppSecretSmsProperties, codeProperties);
        RedissonSmsCodeStore registerSmsCodeStore = new RedissonSmsCodeStore(redissonClient, codeProperties);
        return new SmsCodeClient(registerSmsCodeStore, codeProperties, aliyunSmsClient);
    }
}
